<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/27
 * Time: 10:56
 */

namespace app\api\controller;
use think\Controller;
use think\Request;
use think\Db;
class Activity extends Common
{
    private static $result_data = array(
        'code' => '0',
        'msg' => 'Parameter error',
        'data' => []
    );

    /**
     * 转盘
     */
    public function Turntable(){
        if (Request::instance()->isPost()) {
            $uid = input('post.uid');
            $sKey = input('post.sKey');
            $get_number = input('post.get_number');
            if(empty($uid)){
                self::$result_data['msg'] = '用户ID不能为空';
                return json_encode(self::$result_data);
            }else{
                $u_where = [
                    'uid'=>$uid,
                    'S_key'=>$sKey,
                ];
                $u = Db::table('role_info')->where($u_where)->field('uid')->find();
                if(empty($u)){
                    self::$result_data['msg'] = '用户ID错误';
                    return json_encode(self::$result_data);
                }
            }

            if(empty($get_number)){
                self::$result_data['msg'] = '参数错误';
                return json_encode(self::$result_data);
            }

            //是否实名
            $is_real = Db::table('account_info')->where('uid',$uid)->field('is_real')->find();
            if($is_real['is_real'] == 0){
                self::$result_data['msg'] = '请实名认证';
                return json_encode(self::$result_data);
            }

            $get_number = explode('钻石',$get_number);
            $get_number = $get_number[0];
            //检测当天此用户是否已经抽过奖
            $where = [
                'uid'=>$uid,
                'get_time'=>date('Y-m-d')
            ];
            $res = Db::table('activity_turntable_history')->where($where)->find();
            if(!$res){
                // 启动事务
                Db::startTrans();
                try{
                    //插入转盘表
                    $insert_data = [
                        'uid'=>$uid,
                        'get_time'=>date('Y-m-d'),
                        'get_number'=>$get_number,
                        'created_time'=>date('Y-m-d H:i:s')
                    ];
                    Db::table('activity_turntable_history')->insert($insert_data);
                    /*
                    //插入日志记录表
                    $detail = DB::table('treasure_carddetails')->where('Uid',$uid)->order('DetailsID desc')->find();
                    $CurrentNum = $detail ? $detail['CurrentNum'] : 0;
                    //插入日志记录
                    $log_data = [
                        'Uid'=>$uid,
                        'OperatType'=>13,
                        'CardNum'=>$get_number,
                        'CurrentNum'=>$CurrentNum+$get_number,
                    ];
                    Db::table('treasure_carddetails')->insert($log_data);

                    //更新用户钻石数量
                    $updateNum = [
                        'RoomCardNum'=>$log_data['CurrentNum']
                    ];
                    Db::table('role_info')->where('uid',$uid)->update($updateNum);*/
                    Db::execute('call setRoomCardNum('.$uid.','.$get_number.',13)');
                    Db::commit();
                    self::$result_data['msg'] = '恭喜你获得'.$get_number."钻石";
                    self::$result_data['code'] = $get_number;
                    return json_encode(self::$result_data);
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                }
            }else{
                self::$result_data['msg'] = "抱歉，您今天已经抽过奖了";
                return json_encode(self::$result_data);
            }
        }else{
            $uid = input('get.uid');
            $sKey = input('get.sKey');
            if(empty($uid)){
                echo '参数错误,uid为空';exit;
            }
            if(empty($sKey)){
                echo '参数错误,sKey为空';exit;
            }
            $u_where = [
                'uid'=>$uid,
                'S_key'=>$sKey,
            ];
            $u = Db::table('role_info')->where($u_where)->field('uid')->find();
            if(empty($u)){
                echo '参数错误';exit;
            }
            //获取转盘配置参数
            $config_name = Db::table('activity_config')->where('config_name','turntable')->find();
            if(!$config_name){
                echo '转盘配置参数错误';exit;
            }
            $config_val = explode(',',$config_name['config_val']);
            array_walk(
                $config_val,
                function(&$value, $key, $prefix){$value = $value.$prefix;},
                '钻石'
            );
            //今天是否已抽奖
            $where = [
                'uid'=>$uid,
                'get_time'=>date('Y-m-d')
            ];
            $res = Db::table('activity_turntable_history')->where($where)->find();
            if($res){
                $hint = '今日已获得'.$res['get_number'].'钻石';
            }else{
                //是否实名
                $is_real = Db::table('account_info')->where('uid',$uid)->field('is_real')->find();
                if($is_real['is_real'] == 0){
                    $hint = '请实名认证';
                }else{
                    $hint = '今日免费抽1次';
                }
            }
            $this->assign('uid',$uid);
            $this->assign('sKey',$sKey);
            $this->assign('hint',$hint);
            $this->assign('config_val',implode(',',$config_val));
            return $this->fetch();

        }
    }
}